/**
 * 
 */
package com.baijia.tianxiao.push.web.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baijia.tianxiao.push.model.PushMessage;
import com.baijia.tianxiao.push.result.BaseApiResult;
import com.baijia.tianxiao.push.result.BasePushResult;
import com.baijia.tianxiao.push.service.PushMessageService;

/**
 * @author leiruiqi
 *
 */

@RequestMapping("/pushMessage")
@RestController
public class PushMessageController {

	@Resource
	private PushMessageService pushMessageService;
	
	@RequestMapping("/single")
	public BaseApiResult pushSingle(String bizType,String uid,String deviceId,String message){
		BaseApiResult apiResult = new BaseApiResult();
		
		PushMessage pushMessage = new PushMessage();
		
		pushMessage.setUserId(uid);
		pushMessage.setBizType(bizType);
		pushMessage.setDeviceId(deviceId);
		pushMessage.setMessageInfo(message);
		
		
		
		if(message == null ){
			apiResult.setCode("0");
			apiResult.setMsg("params Invalid");
		}
		
		BasePushResult pushResult = pushMessageService.pushSingle(pushMessage);
		if(pushResult.isSuccess()){
			apiResult.setCode("1");
			return apiResult;
		}else{
			apiResult.setCode("0");
			apiResult.setMsg(pushResult.getMsg());
			return apiResult;
		}
	}
	
	private PushMessage jsonStr2PushMessage(String jsonStr){
		PushMessage message = null;
		try {
			message = JSON.parseObject(jsonStr, PushMessage.class);
		} catch (Exception e) {
			
		}
		return message;
	}
	
	@RequestMapping("/singleForUserIdPrefix")
	public BaseApiResult pushSingleForUserIdPrefix( String bizType,String userId,String message){
		PushMessage pushMessage = new PushMessage();
		
		pushMessage.setUserId(userId);
		pushMessage.setBizType(bizType);
		pushMessage.setMessageInfo(message);
		
		BaseApiResult apiResult = new BaseApiResult();
		BasePushResult pushResult = pushMessageService.pushSingleByUserPrefix(pushMessage);
		if(pushResult.isSuccess()){
			apiResult.setCode("1");
			return apiResult;
		}else{
			apiResult.setCode("0");
			apiResult.setMsg(pushResult.getMsg());
			return apiResult;
		}
	}
	
	
}
